package org.bbelovic.devel.exercises;

public abstract class BinarySearchUtils {

    /**
     * Utility class - no instances allowed
     */
    private BinarySearchUtils() {
        
    }

    /**
     * Generates ordered (in ascending order) integer sequence, value of first item is specified by firstValue parameter,
     * last item of sequence has value lastValue. Items of sequence can repeat forming
     * a subsequence within main sequence. Length of subsequence is controlled via subSequenceLength
     * parameter. Step between item values is one.
     * 
     * Examples of generated sequences:
     * <ul>
     * <li> firstValue, 1, 2, 3, 4, 5,..., lastValue</li>
     * <li> firstValue, firstValue, firstValue, 1, 1, 1, 2, 2, 2,... , lastValue - 1, lastValue - 1, lastValue - 1</li>
     * </ul>
     * @param firstValue value of first item
     * @param lastValue value of last item
     * @param subSequenceLength subsequence length
     * @throws IllegalArgumentException if firstValue > lastValue
     * @return integer sequence ordered in ascending order with first item having value firstValue and last item having value
     * lastValue
     */
    public static int [] generateInputArray(final int firstValue, final int lastValue, final int subSequenceLength) {
        if (firstValue > lastValue) {
            throw new IllegalArgumentException("firstValue > lastValue");
        }
        final int length = lastValue - firstValue + 1;
        final int [] arr = new int [length * subSequenceLength];
        int value = firstValue;
        for (int i = 0; i < arr.length; i += subSequenceLength) {
            for (int j = i; j < i + subSequenceLength; j++) {
                arr [j] = value;
            }
            value++;
        }
        return arr;
    }
}
